<template>
	<view class="container">
		<view class="courses-list">
			<course-row v-for="item in listData" :key="item.id" :item="item" />
		</view>
		<view class="empty-box">
			<u-empty v-if="loadData.status === 'nodata'" text="暫無數據" mode="data" icon="https://cdn.uviewui.com/uview/empty/data.png" />
			<u-loadmore
				v-else
				loadingIcon="circle"
				color="#aaa"
				:status="loadData.status"
				:loading-text="loadData.loadingText"
				:nomore-text="loadData.nomoreText"
			/>
		</view>
		<!-- 登录弹窗 -->
		<login-pop />
		<!-- 协议弹窗 -->
		<protocol-pop />
	</view>
</template>

<script>
	import CourseRow from '@/components/Course/row'
	import { queryBtCourse } from '@/api/course'
	export default {
		components: {
			CourseRow
		},
		data() {
			return {
				listParams: {
					hot: 1,
					sort: 'sorder',
					sortType: 'desc',
					pageNum: 1,
					pageSize: 10
				},
				listData: [],
				loadData: {
					status: 'loading',
					loadingText: '努力加載中',
					nomoreText: '沒有更多了'
				}
			}
		},
		async onLoad() {
			this.initPage()
		},
		methods: {
			async getList() {
				const { pageNum, pageSize } = this.listParams
				const { data } = await queryBtCourse(this.listParams, { needLoading: false })
				if (data.result.length === 0 && pageNum === 1) {
					this.loadData.status = 'nodata'
					this.listData = []
					return
				}
				if (data.result.length < pageSize) {
					this.loadData.status = 'nomore'
				}
				this.listParams.pageNum++
				this.listData = this.listData.concat(data.result)
			},
			async initPage() {
				this.listData = []
				Object.assign(this.listParams, {
					pageNum: 1
				})
				this.loadData.status = 'loading'
				await this.getList()
			}
		},
		onReachBottom() {
			if (this.loadData.status === 'loading') {
				this.getList()
			}
		},
		async onShareTimeline() {
			return {
				title: '熱門課程',
				path: `/pages/course/popular`
			}
		},
		async onShareAppMessage() {
			return {
				title: '熱門課程',
				path: `/pages/course/popular`
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: #fff;
		padding-bottom: 20rpx;
		box-sizing: border-box;
	}

	.courses-list {
		padding: 20rpx;
	}
	/deep/ .empty-box .u-empty {
		height: 70vh;
	}
</style>